<?php

class User extends Font_Controller {
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{
		$this->load->model('user_model');
		if ( ! $this->user_model->is_login())
		{
			msg('请登录！', site_url('user/login'));
		}
		
		echo '用户名：'.$this->session->get('username');
		echo '邮箱：'.$this->session->get('email');
		echo 'IP：'.$this->session->get('ip');
	}
	
	//注册
	public function register()
	{
		if (is_get())
		{
			$this->register_get();
		}
		else
		{
			$this->register_post();
		}
	}
	
	//注册页面
	protected function register_get()
	{
		$this->template->display('register');
	}
	
	//注册提交
	protected function register_post()
	{
		$username = post('username');
		$password = post('password');
		$email = post('email');
		$source = post('source');
		$verify = post('verify');
		
		$this->load->model('user_model');
		if ( ! $this->user_model->register($username, $password, $email, $source, $verify))
		{
			msg($this->user_model->get_error(), -1);
		}
		
		$this->template->display('register_success');
	}
	
	//登录
	public function login()
	{
		if (is_get())
		{
			$this->login_get();
		}
		else
		{
			$this->login_post();
		}
	}
	
	//登录页面
	protected function login_get()
	{
		$this->template->display('login');
	}
	
	//登录提交
	protected function login_post()
	{
		$username = post('username');
		$password = post('password');
		
		$this->load->model('user_model');
		if (FALSE===$this->user_model->login($username, $password))
		{
			msg($this->user_model->get_error(), -1);
		}
		
		msg('登录成功！', site_url('user'));
	}
	
	//注销
	public function logout()
	{
		$this->load->model('user_model');
		$this->user_model->logout();
		msg('成功注销！', site_url('user/login'));
	}
	
	//发送激活邮件
	public function send_activate_email()
	{
		$user_id = intval(segment(3));
		
		$this->load->model('user_model');
		
		if ( ! $this->user_model->send_activate_email($user_id))
		{
			exit($this->user_model->get_error());
		}
		
		exit('发送成功！');
	}
	
	//激活
	public function activate()
	{
		$user_id = intval(segment(3));
		$email_key = trim(segment(4));
		
		$this->load->model('user_model');
		
		if ( ! $this->user_model->activate($user_id, $email_key))
		{
			$this->template->assign('error', $this->user_model->get_error());
			$this->template->display('activate_failure');
		}
		
		$this->template->display('activate_success');
	}
	
	//发送重置密码邮件
	public function send_reset_password_email()
	{
		$user_id = intval(segment(3));
		
		$this->load->model('user_model');
		
		if ( ! $this->user_model->send_reset_password_email($user_id))
		{
			exit($this->user_model->get_error());
		}
		
		exit('发送成功！');
	}
	
	//验证码
	public function verify()
	{
		$this->load->library('image');
		$config = array(
			'name' => 'verify'
		);
		$this->image->code($config);
	}

}